package com.app.newcsc.fragment.my.level;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.app.AppViewModelFactory;
import com.app.newcsc.databinding.FragmentLevelUpBinding;
import com.app.newcsc.databinding.FragmentMyPaytypeBinding;
import com.app.newcsc.entity.LevelInfoEntity;
import com.app.newcsc.entity.UserEntity;
import com.app.newcsc.utils.NumberUtils;
import com.app.newcsc.utils.SpUtils;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LevelUpFragment extends BaseFragment<FragmentLevelUpBinding, LevelUpViewModel> {

    private int mLevel = 0;
    private int mPowerLevel = 0;
    private boolean isCanClick = true;

    @Override
    public void initParam() {
        super.initParam();
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_level_up;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LevelUpViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(LevelUpViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        SpUtils.put(getActivity(), "isLogin", "1");
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        //发起网络请求
        viewModel.baseUser();
        viewModel.levelInfo();

        viewModel.mUserInfoEntity.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity userInfoEntity) {
                initExchange(userInfoEntity);
            }
        });

        viewModel.mLevelInfoEntity.observe(this, new Observer<LevelInfoEntity>() {
            @Override
            public void onChanged(LevelInfoEntity levelInfoEntity) {
                initLevel(levelInfoEntity);
            }
        });

        viewModel.successStr.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String str) {
                if (str.equals("1")) {
                    ToastUtils.showShort("兑换成功");
                    viewModel.baseUser();
                    viewModel.levelInfo();
                }else {
                    isCanClick = true;
                }
            }
        });

        binding.tvExchangeOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPowerLevel == 0) {
                    if (mLevel == 0) {
                        if (isCanClick) {
                            isCanClick = false;
                            viewModel.upGrade();
                        }
                    } else {
                        ToastUtils.showShort("不符合兑换等级要求");
                    }
                }
            }
        });

        binding.tvExchangeTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPowerLevel == 1) {
                    if (mLevel == 1) {
                        if (isCanClick) {
                            isCanClick = false;
                            viewModel.upGrade();
                        }

                    } else {
                        ToastUtils.showShort("不符合兑换等级要求");
                    }
                }
            }
        });

        binding.tvExchangeThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPowerLevel == 2) {
                    if (mLevel == 2) {
                        if (isCanClick) {
                            isCanClick = false;
                            viewModel.upGrade();
                        }
                    } else {
                        ToastUtils.showShort("不符合兑换等级要求");
                    }
                }
            }
        });

        binding.tvExchangeFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPowerLevel == 3) {
                    if (mLevel == 3) {
                        if (isCanClick){
                            viewModel.upGrade();
                        }
                    } else {
                        ToastUtils.showShort("不符合兑换等级要求");
                    }
                }
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void initLevel(LevelInfoEntity levelInfoEntity) {
        double adNum = Double.valueOf(levelInfoEntity.getAd().getMoney()) - Double.valueOf(levelInfoEntity.getAd().getLock_s_one());
        binding.tvAdNum.setText(NumberUtils.keepPrecision(String.valueOf(adNum), 4));
        binding.tvPriceOne.setText(levelInfoEntity.getPrice_list().get_$1() + "AD");
        binding.tvPriceTwo.setText(levelInfoEntity.getPrice_list().get_$2() + "AD");
        binding.tvPriceThree.setText(levelInfoEntity.getPrice_list().get_$3() + "AD");
        binding.tvPriceFour.setText(levelInfoEntity.getPrice_list().get_$4() + "AD");
    }

    private void initExchange(UserEntity userInfoEntity) {
        mLevel = userInfoEntity.level;
        mPowerLevel = userInfoEntity.power_level;
        switch (mPowerLevel) {
            case 0:
                break;
            case 1:
                binding.tvExchangeOne.setText("已兑换");
                binding.tvExchangeOne.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);

                binding.tvExchangeTwo.setText("兑换");
                if (mLevel == 1 && mPowerLevel == 1) {
                    binding.tvExchangeTwo.setBackgroundResource(R.drawable.shape_radius_5_1fabcc_3212cf);
                    binding.tvNotLevelTwo.setVisibility(View.GONE);
                } else {
                    binding.tvExchangeTwo.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);

                }

                binding.tvExchangeThree.setText("兑换");
                binding.tvExchangeThree.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvExchangeFour.setText("兑换");
                binding.tvExchangeFour.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                break;
            case 2:
                binding.tvExchangeOne.setText("已兑换");
                binding.tvExchangeOne.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvExchangeTwo.setText("已兑换");
                binding.tvExchangeTwo.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvNotLevelTwo.setVisibility(View.GONE);

                binding.tvExchangeThree.setText("兑换");
                if (mLevel == 2 && mPowerLevel == 2) {
                    binding.tvExchangeThree.setBackgroundResource(R.drawable.shape_radius_5_1fabcc_3212cf);
                    binding.tvNotLevelThree.setVisibility(View.GONE);
                } else {
                    binding.tvExchangeThree.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                }

                binding.tvExchangeFour.setText("兑换");
                binding.tvExchangeFour.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                break;
            case 3:
                binding.tvExchangeOne.setText("已兑换");
                binding.tvExchangeOne.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);

                binding.tvExchangeTwo.setText("已兑换");
                binding.tvExchangeTwo.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvNotLevelTwo.setVisibility(View.GONE);

                binding.tvExchangeThree.setText("已兑换");
                binding.tvExchangeThree.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvNotLevelThree.setVisibility(View.GONE);

                binding.tvExchangeFour.setText("兑换");
                if (mLevel == 3 && mPowerLevel == 3) {
                    binding.tvExchangeFour.setBackgroundResource(R.drawable.shape_radius_5_1fabcc_3212cf);
                    binding.tvNotLevelFour.setVisibility(View.GONE);
                } else {
                    binding.tvExchangeFour.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                }
                break;
            case 4:

                binding.tvExchangeOne.setText("已兑换");
                binding.tvExchangeOne.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);

                binding.tvExchangeTwo.setText("已兑换");
                binding.tvNotLevelTwo.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvNotLevelTwo.setVisibility(View.GONE);

                binding.tvExchangeThree.setText("已兑换");
                binding.tvExchangeThree.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvNotLevelThree.setVisibility(View.GONE);

                binding.tvExchangeFour.setText("已兑换");
                binding.tvExchangeFour.setBackgroundResource(R.drawable.shape_radius_5_d4d4d4_9b9b9b);
                binding.tvNotLevelFour.setVisibility(View.GONE);
                break;
        }

        switch (mLevel) {
            case 0:
                binding.tvLevelName.setText("蚂蚁宝宝");
                break;
            case 1:
                binding.tvLevelName.setText("小蚂蚁");
                break;
            case 2:
                binding.tvLevelName.setText("一级雄兵");
                break;
            case 3:
                binding.tvLevelName.setText("二级雄兵");
                break;
            case 4:
                binding.tvLevelName.setText("三级雄兵");
                break;
        }

        isCanClick = true;
    }
}
